02. Creating an Instance of a Struct

Creating an Instance of a Struct

Creating an Instance of a Struct

To show the alternative method for initializing (and creating) a struct, let’s return to the Student struct, which does not have any default property values:

Create Student Struct

This code creates an instance of the Student struct called "ayush".

This code creates an instance of the Student struct called "ayush".

More Examples

From lines 1 to 4, we provide the definition for the Student struct, but still need to actually create an actual Student. On line 7, we create an instance of the Student struct called ayush and set its initial values all at the same time! An instance is the actual realization of a type. Recalling the blueprint analogy, the blueprint itself is like the definition of the Student struct and an actual engine built from the blueprint is an instance.

var ayush = Student(name: "Ayush Saraswat", age: 19, school: "Udacity")

We use the keyword var here because like a variable, this instance can be mutated, or changed. The rest of the line is new syntax, but it reads quite nicely. We have the word Student which is the data type. Next, we have a list of the property names followed by colons and literal values. That’s it. With one statement we’ve created an instance of the Student struct and set its initial values.

Talking About Instances

When we talk about this new instance named ayush, we would say that “ayush is a Student” or “ayush is an instance of the Student type”. This is much more powerful than saying something like “ayush contains a name of type String, an age of type Int, and a school of type String”. All that information is now implied when you say “ayush is a Student”.

A Default Constructor

var ayush = Student(name: "Ayush Saraswat", age: 19, school: "Udacity")

You may be wondering, where did this fancy line of code come from? How the heck was I supposed to know to use that syntax? Well, this statement is what's known as a default initializer or default constructor. When we defined the Student struct, the Swift compiler created the default constructor for us so that we could use it to create Student instances.

More Constructor Examples

If we look back at our other struct definitions for GeoLocation and Point2D, we now know that we can use their default constructors to create instances of those structs.

struct Point2D {
    var x: Int = 0
    var y: Int = 0
}

struct GeoLocation {
    var latitude: Double = 0.0
    var longitude: Double = 0.0
}

var characterPosition = Point2D(x: 10, y: 10)
let udacityCoordinates = GeoLocation(latitude: 37.400073, longitude: -122.108400)

Here, we create a Point2D named characterPosition, and then we create a GeoLocation named udacityCoordinates. Try recreating these structs (or your own structs) and then use their default constructors.

If you have any issues defining structs like Student, GeoLocation, or Point2D and creating an instance of a struct using its default constructors, let a coach know on the forums.